import React from 'react';
import ConfigTable from 'lib/ConfigTable/Action';
import axios from 'axios';
import {message} from 'antd';


export default class extends ConfigTable {
    editModelInfo = (rowData, index) => {
        this.models.modelInfoM.set({
            formData: rowData,
            editInfoModel: index
        });
        this.actions.dmPageA.turnPage('info');
    }

    editModelData = async (rowData, index) => {
        const res = await axios.get('/getModelData');
        const modelDataCollection = this.models.dataTM.get('modelDataCollection');

        let keys = [rowData.keyName];
        this.actions.dmPageA.turnPage('detail');
        
        this.models.dataTM.set({
            currentModelKey: rowData.keyName,
            keys: keys,
            [`modelDataCollection.${rowData.keyName}`]: res.data.data[rowData.keyName]
        });
        this.actions.dataTA.setModelData();
    } 

    uploadModel = (rowData, index) => {
        const {modelListM, dataTM} = this.models;
        let currentModelKey = '';
        let modelInfo = null;
        
        if (rowData) {
            currentModelKey = rowData.keyName;
            modelInfo = rowData;
        } else {
            const tableData = modelListM.get('data');
            currentModelKey = dataTM.get('currentModelKey');
            modelInfo = tableData.filter(item => item.keyName === currentModelKey)[0];
        }

        const modelData = dataTM.get(`modelDataCollection.${currentModelKey}`);
        
        axios.post('/saveModelData', {
            modelData: modelData,
            modelInfo: modelInfo 
        })
            .then((res) => {
                if (res.data.success) {
                    message.success('保存成功');
                }
            });
    }

    getModelList = () => {
        axios.get('/getModelList')
            .then((res) => {
                if (res.data.success) {
                    const {data} = res.data;
                    const modelList = Object.values(data);

                    this.models.modelListM.set({
                        data: modelList 
                    });
                }
            });
    }

    deleteModel = (rowData, index) => {
        axios.post('/deleteModel', {
            modelName: rowData.keyName
        })
            .then((res) => {
                if (res.data.success) {
                    message.success('删除成功');
                    this.getModelList();
                }
            });
    }

    makeModelCode = (rowData, index) => {
        axios.post('/makeModelCode', {
            modelName: rowData.keyName
        })
            .then((res) => {
                message.success('制作成功');
            });
    }
}